package com.cssw.fyzb.busi.api.task;

import com.cssw.fyzb.busi.service.task.TaskService;
import com.cssw.fyzb.busi.util.BizUtil;
import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.core.model.vo.task.TaskDescDataVo;
import com.cssw.fyzb.core.model.vo.task.TaskDescVo;
import com.cssw.fyzb.core.model.vo.task.TodayTaskRecordVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Function(description = "根据类型获取任务描述", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = false),
        @Parameter(name = "taskType", type = Type.String, description = "任务类型（1-书卷任务 2-游戏任务 3-转发任务 4-活动任务 5-广告任务）", required = true)
})
@Component
public class GetTaskDescByType implements Action {

    private final static Logger logger = LoggerFactory.getLogger(GetTaskDescByType.class);

    @Autowired
    private TaskService taskService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        String taskType = request.getString("taskType");
        try {
            TaskDescVo vo = new TaskDescVo();
            if (GzAddPrice.TASK_TYPE_ADVERT.equals(taskType)){
                vo.setTitle("广告任务说明");
                StringBuffer content = new StringBuffer();
                content.append("1. 在广告页面，点击看广告图片，观看广告；").append("\n")
                        .append("2. 累计观看广告次数达到标准，即可增值平安果；").append("\n")
                        .append("3. 增值与广告次数标准：");
                vo.setContent(content.toString());
                List<AdNumPrice> list = taskService.listAdNumPrice();
                List<TaskDescDataVo> dataVos = new ArrayList<>();
                TaskDescDataVo dataVo = null;
                for (AdNumPrice numPrice: list){
                    dataVo = new TaskDescDataVo();
                    dataVo.setNum(numPrice.getNum().toString());
                    dataVo.setPrice(numPrice.getPrice().toString());
                    dataVos.add(dataVo);
                }
                vo.setDataList(dataVos);
                vo.setNumName("观看数量");
                vo.setPriceName("增值金额");

            } else if (GzAddPrice.TASK_TYPE_TURNTABLE.equals(taskType)){
                vo.setTitle("活动任务说明");
                StringBuffer content = new StringBuffer();
                content.append("1. 在活动页面，点击抽奖，将消耗10福币进行抽奖；").append("\n")
                        .append("2. 累计抽奖次数达到标准，即可增值平安果；").append("\n")
                        .append("3. 增值与抽奖次数标准：");
                vo.setContent(content.toString());
                List<TurntableNumPrice> list = taskService.listTurntableNumPrice();
                List<TaskDescDataVo> dataVos = new ArrayList<>();
                TaskDescDataVo dataVo = null;
                for (TurntableNumPrice numPrice: list){
                    dataVo = new TaskDescDataVo();
                    dataVo.setNum(numPrice.getNum().toString());
                    dataVo.setPrice(numPrice.getPrice().toString());
                    dataVos.add(dataVo);
                }
                vo.setDataList(dataVos);
                vo.setNumName("转盘次数");
                vo.setPriceName("增值金额");
            } else {
                BizUtil.exception("没有该任务说明，请联系客服！");
            }

            return Response.success().put("data", vo);
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.error("", e);
            return Response.fail("查询失败");
        }
    }

}
